﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDADiscount
    {
        public static List<Discount> Select(Discount discount, bool isFilterIsActive = false)
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            string NotEffective = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString();
            con.Open();
            string _cmdText =
                            "SELECT d.* " +
                            "FROM discount d " +
                            "WHERE ((d.ID = " + discount.ID.ToString() + ") OR (" + discount.ID.ToString() + " = " + NotEffective + "))" +
                            "  AND ((d.CompanyID=" + discount.Company.ID.ToString() + ")OR(" + discount.Company.ID.ToString() + "=" + NotEffective + "))" +
                            "  AND ((d.IsActive = " + Convert.ToInt32(discount.IsActive).ToString() + ") OR (" + Convert.ToInt32(isFilterIsActive).ToString() + " = 0)) " +
                            "ORDER BY d.GroupingCode, d.ID";

            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<Discount> discountList = new List<Discount>();

            foreach (DataRow row in tbl.Rows)
            {
                Discount _discount = new Discount();
                _discount.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));

                _discount.Company = new Company();
                _discount.Company.ID = (row["CompanyID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["CompanyID"]));

                _discount.GroupingCode = (row["GroupingCode"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["GroupingCode"].ToString()));
                _discount.ForDiscountElementID = (row["For_DiscountElementID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["For_DiscountElementID"].ToString()));
                _discount.ForDiscountElementValue = (row["For_DiscountElementValue"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["For_DiscountElementValue"].ToString()));
                _discount.ForCount = (row["For_Count"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToDecimal(row["For_Count"].ToString()));
                _discount.DiscountDiscountElementID = (row["Discount_DiscountElementID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["Discount_DiscountElementID"].ToString()));
                _discount.DiscountDiscountElementValue = (row["Discount_DiscountElementValue"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["Discount_DiscountElementValue"].ToString()));
                _discount.DiscountCount = (row["Discount_Count"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToDecimal(row["Discount_Count"].ToString()));
                _discount.IsActive = (row["IsActive"].ToString() == "1" ? true : false);
                _discount.ActiveDays = (row["ActiveDays"].ToString() == "" ? null : row["ActiveDays"].ToString());
                if (row["ActiveHourFrom"].ToString() != "")
                    _discount.ActiveHourFrom = Convert.ToDateTime("1999-01-01 " + row["ActiveHourFrom"]);
                if (row["ActiveHourTo"].ToString() != "")
                    _discount.ActiveHourTo = Convert.ToDateTime("1999-01-01 " + row["ActiveHourTo"]);
                
                discountList.Add(_discount);
            }
            con.Close();
            return discountList;
        }
    }
}